Seurat

数据来源:https://support.10xgenomics.com/spatial-gene-expression/datasets/1.0.0/V1_Human_Lymph_Node

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)
library(Seurat)
## Attaching SeuratObject
data <- Load10X_Spatial("~/Downloads/V1_Human_Lymph_Node/", filename = "V1_Human_Lymph_Node_filtered_feature_bc_matrix.h5")

SpatialFeaturePlot(data, features = "CR2")

SpatialDimPlotSpatialFeaturePlot会产生一个patchwork/ggplot的对象,所以我们可以按照ggplot的方式添加图层。首先构建一个有每个spot位置信息(GetTissueCoordinates)和表达量信息(FetchData)的data.frame。

feature.data <- FetchData(data, c("CR2", "CLU")) %>% merge(GetTissueCoordinates(data), by=0)
head(feature.data)
##            Row.names CR2 CLU  imagerow  imagecol
## 1 AAACAAGTATCTCCCA-1   8   7 356.31538 425.97601
## 2 AAACAATCTACTAGCA-1   2  13  69.50753 217.96376
## 3 AAACACCAATAACTGC-1   2  18 412.04388 134.52411
## 4 AAACAGAGCGACTCCT-1  26  21 136.31028 397.29522
## 5 AAACAGCTTTCAGAAG-1  37  19 314.31487  99.10691
## 6 AAACAGGGTCTATATT-1   4  23 338.75988 113.24317

geom_star添加(画半圆需要最新github版本的ggstar)

library(ggplot2)
library(ggstar)
library(ggnewscale)
SpatialDimPlot(data, pt.size.factor = 0) + NoLegend() + 
  new_scale_fill() +
  geom_star(data=feature.data, aes(x=imagecol, y=576-imagerow, fill=CR2), 
            inherit.aes = F, starshape=31, size=2.5, alpha=0.5)  +
  scale_fill_gradient(low="darkblue", high="red") +
  new_scale_fill() + 
  geom_star(data=feature.data, aes(x=imagecol, y=576-imagerow, fill=CLU), 
            inherit.aes = F, starshape=31, size=2.5, angle=180, alpha=0.5)  +
  scale_fill_gradient(low="darkblue", high="green")

更深入的理解需要明白两个重要的坐标变换:(1)imagerowimagecol是如何计算出来的;(2)作图时如何将他们对应到图像上。

这些重要的位置信息其实可以在spaceranger的spatial/文件夹下找到

cd ~/Downloads/V1_Human_Lymph_Node
tree spatial/
file spatial/tissue_lowres_image.png

cat spatial/scalefactors_json.json
## spatial/
## ├── aligned_fiducials.jpg
## ├── detected_tissue_image.jpg
## ├── scalefactors_json.json
## ├── tissue_hires_image.png
## ├── tissue_lowres_image.png
## └── tissue_positions_list.csv
## 
## 0 directories, 6 files
## spatial/tissue_lowres_image.png: PNG image data, 576 x 600, 8-bit/color RGB, non-interlaced
## {"spot_diameter_fullres": 89.49502418224989, "tissue_hires_scalef": 0.17011142, "fiducial_diameter_fullres": 144.56888521748058, "tissue_lowres_scalef": 0.051033426}

从上面GetTissueCoordinates的输出中我们看到barcodeAAACAAGTATCTCCCA-1对应的位置是(imagerow=356.31538, imagecol=425.97601)。在10X原始位置信息中我们可以找到这个barcode的对应位置:

read_csv("~/Downloads/V1_Human_Lymph_Node/spatial/tissue_positions_list.csv", col_names = F) %>% 
  filter(X1=="AAACAAGTATCTCCCA-1")
## Rows: 4992 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): X1
## dbl (5): X2, X3, X4, X5, X6
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## # A tibble: 1 × 6
##   X1                    X2    X3    X4    X5    X6
##   <chr>              <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAACAAGTATCTCCCA-1     1    50   102  6982  8347

对应的坐标为(6982, 8347),从这个坐标换算到lowres图像的系数为JSON文件中的0.051033426

read_csv("~/Downloads/V1_Human_Lymph_Node/spatial/tissue_positions_list.csv", col_names = F) %>% 
  filter(X1=="AAACAAGTATCTCCCA-1") %>% 
  select(X1, X5, X6) %>% 
  mutate_if(is.numeric, ~ .x*0.051033426)
## Rows: 4992 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): X1
## dbl (5): X2, X3, X4, X5, X6
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## # A tibble: 1 × 3
##   X1                    X5    X6
##   <chr>              <dbl> <dbl>
## 1 AAACAAGTATCTCCCA-1  356.  426.

在作图的时候需要注意ximagecoly为翻转的imagerow,也就是576-imagerow,其中576为图像的高度

执行信息

sessionInfo()
## R version 4.2.3 (2023-03-15)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur ... 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggnewscale_0.4.9   ggstar_1.0.4       ggplot2_3.4.1      SeuratObject_4.1.3
## [5] Seurat_4.3.0       readr_2.1.4        dplyr_1.1.1       
## 
## loaded via a namespace (and not attached):
##   [1] Rtsne_0.16             colorspace_2.1-0       deldir_1.0-6          
##   [4] ellipsis_0.3.2         ggridges_0.5.4         rstudioapi_0.14       
##   [7] spatstat.data_3.0-1    farver_2.1.1           leiden_0.4.3          
##  [10] listenv_0.9.0          bit64_4.0.5            ggrepel_0.9.3         
##  [13] fansi_1.0.4            codetools_0.2-19       splines_4.2.3         
##  [16] cachem_1.0.7           knitr_1.42             polyclip_1.10-4       
##  [19] jsonlite_1.8.4         ica_1.0-3              cluster_2.1.4         
##  [22] png_0.1-8              uwot_0.1.14            shiny_1.7.4           
##  [25] sctransform_0.3.5      spatstat.sparse_3.0-1  compiler_4.2.3        
##  [28] httr_1.4.5             Matrix_1.5-3           fastmap_1.1.1         
##  [31] lazyeval_0.2.2         cli_3.6.1              later_1.3.1           
##  [34] htmltools_0.5.5        tools_4.2.3            igraph_1.4.2          
##  [37] gtable_0.3.3           glue_1.6.2             RANN_2.6.1            
##  [40] reshape2_1.4.4         Rcpp_1.0.10            scattermore_1.0       
##  [43] jquerylib_0.1.4        vctrs_0.6.1            nlme_3.1-162          
##  [46] spatstat.explore_3.1-0 progressr_0.13.0       lmtest_0.9-40         
##  [49] spatstat.random_3.1-4  xfun_0.39              stringr_1.5.0         
##  [52] globals_0.16.2         mime_0.12              miniUI_0.1.1.1        
##  [55] lifecycle_1.0.3        irlba_2.3.5.1          goftest_1.2-3         
##  [58] future_1.32.0          MASS_7.3-58.2          zoo_1.8-12            
##  [61] scales_1.2.1           vroom_1.6.1            hms_1.1.3             
##  [64] promises_1.2.0.1       spatstat.utils_3.0-2   parallel_4.2.3        
##  [67] RColorBrewer_1.1-3     yaml_2.3.7             reticulate_1.28       
##  [70] pbapply_1.7-0          gridExtra_2.3          sass_0.4.5            
##  [73] stringi_1.7.12         highr_0.10             rlang_1.1.0           
##  [76] pkgconfig_2.0.3        matrixStats_0.63.0     evaluate_0.20         
##  [79] lattice_0.20-45        tensor_1.5             ROCR_1.0-11           
##  [82] purrr_1.0.1            labeling_0.4.2         patchwork_1.1.2       
##  [85] htmlwidgets_1.6.2      bit_4.0.5              cowplot_1.1.1         
##  [88] tidyselect_1.2.0       parallelly_1.35.0      RcppAnnoy_0.0.20      
##  [91] plyr_1.8.8             magrittr_2.0.3         R6_2.5.1              
##  [94] generics_0.1.3         DBI_1.1.3              withr_2.5.0           
##  [97] pillar_1.9.0           fitdistrplus_1.1-11    abind_1.4-5           
## [100] survival_3.5-3         sp_1.6-0               tibble_3.2.1          
## [103] future.apply_1.10.0    crayon_1.5.2           hdf5r_1.3.8           
## [106] KernSmooth_2.23-20     utf8_1.2.3             spatstat.geom_3.1-0   
## [109] plotly_4.10.1          tzdb_0.3.0             rmarkdown_2.20        
## [112] grid_4.2.3             data.table_1.14.8      digest_0.6.31         
## [115] xtable_1.8-4           tidyr_1.3.0            httpuv_1.6.9          
## [118] munsell_0.5.0          viridisLite_0.4.1      bslib_0.4.2